layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'upload', 'table'], function () {
    var form = layui.form,
        upload = layui.upload,
        laypage = layui.laypage,
        table  = layui.table,
        admin = layui.admin;


    var apply = false;

    // 文件标题弹出输入开始
    $('.Aup_img').click(function () {
        $('.alert').fadeIn();
    });
    $('.alert_cancel').click(function () {
        $('.alert').fadeOut();
        $('.alert input').val('');
        $('#up_img').hide();
    });
    // 文件标题弹出输入结束

    // 判断标题是否为空开始
    $('.alert input').bind('input propertychange', function() {
        if($(this).val()){
            $('#up_img').show();
        }else {
            $('#up_img').hide();
        }
    });
    // 判断标题是否为空结束

    //文件上传
    var imgUpload = upload.render({
        elem: '#up_img'
        ,url: '/advert/fielUpload'
        ,multiple: true
        ,accept: 'file'
        ,size: 51200
        ,data: {ad_title:  function () {
                return $('.alert input').val()
            }, ad_type: ''}
        ,choose: function () {
            $('.alert').fadeOut();
            $('#up_img').hide();
        }
        ,done: function(res){
            //上传完毕
            if (res.code==1){
                $('.alert').fadeOut();
                $('.alert_content input').val('');
                layer.alert('上传成功',{icon: 6}, function(index){
                    location.reload();
                });
            }else {
                layer.alert(res.message)
            }
        }
        , error: function () {
            layer.open({
                content: '上传失败'
                ,icon: 5
                ,btn: ['重传', '取消']
                ,yes: function(index, layero){
                    imgUpload.upload();
                }
                ,btn2: function(index, layero){
                    //按钮【按钮二】的回调

                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
        }
    });
    // 文件上传结束


    // 获取分页总数
    $.ajax({
        type: "POST",
        url: "/advert/queryAdList",
        success: function (res) {
            var count = res.count;
            if (res.code == 1) {
                pageupImgFn(count);
            } else {
                layer.msg(res.message);
            }
        }
    });


    // 查询开始
    $('.search_btn_img').click(function () {
        var checkType = $('.checkType option:selected').val();
        var queryVal = $('.queryImg input').val();
        $.ajax({
            type: "POST",
            url: "/advert/queryAdList",
            data: {
                "ad_type": checkType,
                "ad_title": queryVal
            },
            success: function (res) {
               var queryCount = res.count;
                if (res.code == 1) {
                    pageupImgFn(queryCount,queryVal,checkType)
                } else {
                    layer.msg(res.message);
                }
            }
        });
    });
    // 查询结束

    // 文件分页显示方法
    function pageupImgFn(count,ad_title,ad_type) {
        laypage.render({
            elem: 'pageContent'
            ,count: count
            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
            ,jump: function(obj){
                var pageSize = obj.limit;
                var pageNo = obj.curr;
                $.ajax({
                    type: "POST",
                    url: "/advert/queryAdList",
                    data: {
                        "ad_type": ad_type,
                        "ad_title": ad_title,
                        "pageSize": pageSize,
                        "pageNo": pageNo
                    },
                    success: function (res) {
                        var resData = res.data;
                        if (res.code == 1) {
                            if (res.data.length=="0"){
                                $('#img_display').empty();
                                $('#img_display').append('<span>'+"暂无查询内容"+'</span>');
                                $('#pageContent').hide();
                            }else {
                                $('#pageContent').show();
                                $('#img_display').empty();
                                layui.each(resData, function(index){
                                    if(resData[index].ad_type=="image"){
                                        $('#img_display').append('<div class="fileBox">'
                                            +'<div>'+'<img src="'+ resData[index].url +'" class="layui-upload-img">'
                                            +'</div>'+'<div><span>'+'<b>标题:</b>'+resData[index].ad_title
                                            +'</span><span>'+'<b>时间:</b>'+resData[index].updatedTime+'</span></div>'
                                            +'</div>');
                                    }else if(resData[index].ad_type=="video"){
                                        $('#img_display').append('<div class="fileBox">'
                                            +'<div>'+'<video controls src="'+ resData[index].url
                                            +'" class="layui-upload-img play_video">'+'</video>'+'<span class="v_delete"></span>'
                                            +'<div class="video_bg"><img src="/images/bofang.png"/></div>'+'</div>'+'<div><span>'
                                            +'<b>标题:</b>'+resData[index].ad_title+'</span><span>'+'<b>时间:</b>'
                                            +resData[index].updatedTime+'</span></div>'
                                            +'</div>')
                                    }
                                });
                            }


                        } else {
                            layer.msg(res.message);
                        }
                    }
                });

            }
        });
    }

    // 点击播放视频开始
    $(document).on("click",".video_bg",function(){
        var _this = $(this);
        var video = _this.siblings('video');//视频
        var span = _this.siblings('span');//关闭视频按钮
        var videoBox = _this.parent();//装视频盒子
        video.trigger('play');
        _this.hide();
        videoBox.addClass('enlarge');
        span.click(function () {
            videoBox.removeClass('enlarge');
            video.trigger('pause');
            _this.show();
        });

    });
    // 点击播放视频结束

});





